<?php


namespace app\admin\service;

use app\admin\model\ProjectsDrawingLib;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\IOFactory;
use think\Model;
use app\admin\model\ProjectsOrder as ProjectsOrderM;

class ProjectsOrder extends Model
{
    /**
     * Desc: 项目订单导入excel
     * User: 苏鹏
     * Date: 2023/10/8
     * Time: 12:46
     * @return void
     */
    public function projOrderExcelImport(string $file_path, int $proj_id)
    {
        if (!empty($file_path)) {
            //如果文件存在
            if (file_exists($file_path)) { //检查文件是否存在
                $exts = pathinfo($file_path, PATHINFO_EXTENSION);  //文件类型
                // 有Xls和Xlsx格式两种
                if ($exts == 'xlsx') {
                    $obj_reader = IOFactory::createReader('Xlsx');
                } else {
                    //$objReader = IOFactory::createReader('Xls');
                    return re_error_json(msg_code(21)); //请使用'Xlsx'格式文件进行带图片上传,否则图片上传失败！
                }
                //暂时不处理图片
//                $image_file_path1 = root_path() . '/public';//图片保存目录
//                $image_file_path2 = '/upload/admin/images/' . date("Ymd") . '/';
//                $image_file_path = $image_file_path1 . $image_file_path2;
//                if (!file_exists($image_file_path)) {
//                    mkdir("$image_file_path", 0777, true);
//                }

                $obj_spreadsheet = $obj_reader->load($file_path);
                $obj_worksheet = $obj_spreadsheet->getActiveSheet(0);  //getSheet(0)
                $highestRow = $obj_worksheet->getHighestRow(); // 取得总行数
                $data = $obj_worksheet->toArray();
                //暂时不处理图片
                //$drawing 为 PhpOffice\PhpSpreadsheet\Worksheet\Drawing类的实例;   仅仅支持xlsx格式文件
//                foreach ($obj_worksheet->getDrawingCollection() as $drawing) {
//                    list($start_column, $start_row) = Coordinate::coordinateFromString($drawing->getCoordinates());
//                    $image_file_name = $drawing->getIndexedFilename(); //获取文件名称
//                    switch ($drawing->getExtension()) {
//                        case 'jpg':
//                        case 'jpeg':
//                            $source = imagecreatefromjpeg($drawing->getPath());
//                            imagejpeg($source, $image_file_path . $image_file_name);
//                            break;
//                        case 'gif':
//                        case 'GIF':
//                            $source = imagecreatefromgif($drawing->getPath());
//                            imagegif($source, $image_file_path . $image_file_name);
//                            break;
//                        case 'png':
//                            $source = imagecreatefrompng($drawing->getPath());
//                            imagepng($source, $image_file_path . $image_file_name);
//                            break;
//                        default:
//                            return re_error_json(msg_code(24)); //仅支持jpeg png 格式图片，请修改文件内容。
//                            break;
//                    }
//                    $start_column = $this->ABC2decimal($start_column);
//                    $data[$start_row - 1][$start_column] = $image_file_path2 . $image_file_name;
//                }
                //从第二行开始
                $img_data = [];
                for ($i = 1; $i <= $highestRow - 1; $i++) {
                    $add_data[$i]['name'] = $data[$i][0];
                    $add_data[$i]['categorize'] = $data[$i][1];
                    $add_data[$i]['model_number'] = $data[$i][2];
                    $add_data[$i]['number'] = intval($data[$i][3]);
                    $add_data[$i]['drawing_number'] = $data[$i][4];
                    $add_data[$i]['delivery_date'] = strtotime($data[$i][5]);
                    $add_data[$i]['price'] = $data[$i][6];
                    $add_data[$i]['notes'] = $data[$i][7];
                    $add_data[$i]['drawing_img'] = '';
//                    $add_data[$i]['drawing_img'] = $data[$i][6];
                    $add_data[$i]['status'] = 1;
                    $add_data[$i]['proj_id'] = $proj_id;
                    $add_data[$i]['order_status'] = 1;
                    $add_data[$i]['delivery_date_records'] = '';

//                    $img_data[$i]['image'] = $data[$i][6];
                }
                //暂时不处理图片
                //保存至数据库
                if(!empty($add_data)){
                    (new ProjectsOrderM)->saveAll($add_data);
                }
                //图片保存至资源库
                if(!empty($img_data)) {
                    (new ProjectsDrawingLib)->saveAll($img_data);
                }

            }
        }
    }

    /**
     * @param $abc
     * Desc: 字母序列化数字
     * User: 苏鹏
     * Date: 2023/10/7
     * Time: 15:44
     * @return float|int
     */
    public function ABC2decimal($abc)
    {
        $ten = 0;
        $len = strlen($abc);
        for ($i = 1; $i <= $len; $i++) {
            $char = substr($abc, 0 - $i, 1);//反向获取单个字符
            $int = ord($char);
            $ten += ($int - 65) * pow(26, $i - 1);
        }
        return $ten;
    }
}